<?php
/*
Plugin Name: img_carousel
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: image carousel
Version: 1.0
Author: fox611
Author URI: blog.fox611.com
*/

class widget_img_carousel extends WP_Widget {
    function widget_img_carousel() {
        $widget_ops = array('description' => '实现图片轮播');
        $control_ops = array('width'=>200,'height'=>138);
        $this->WP_Widget('widget_img_carousel', '图片轮播', $widget_ops,$control_ops);
    }
    function widget($args, $instance) {
        extract($args);
        ob_start();
        //$num = apply_filters('widget_img_carousel_num',esc_attr($instance['num']));
        //$img_array = apply_filters('widget_img_carousel_array',esc_attr($instance['img_array']));
        echo $before_widget;
        ?>
        <div class='carousel_window' style="width:1000px; height:210px; position:relative ">
            <div class='carousel carousel_A' style="width:1000px; height:210px; position:absolute">
                <a href='<?php echo $instance['href1']?>'><img class="carousel_1" src='<?php echo $instance['img1']?>' ></a>
            </div>
            <div class='carousel carousel_B' style="width:1000px; height:210px;position:absolute; left:1000px">
                <a href='<?php echo $instance['href2']?>'><img class="carousel_2" src='<?php echo $instance['img2']?>' ></a>
            </div>
        </div>
        <?php
        //echo '<ul class="middleli">';
           //for ($i=1; $i<6; $i++) {
            //    echo '<img class="carousel_'.$i.'" src='.$instance['img'.$i].' style="display:none">';
            //    echo "<hr/>";
           // }
            //test( $limit );    //
        //echo '</ul>';
        echo $after_widget;
        ?>
        <script type="text/javascript">
           var top_img=0;
           var inturn=2;
           var img_array=new Array();
           var href_array=new Array();
           <?php for($i=1;$i<6;$i++)
           {
            echo 'img_array['.$i.']="'.$instance["img".$i].'";';
            echo 'href_array['.$i.']="'.$instance["href".$i].'";';
           }
           ?>
            function carousel()
            {
            $(".carousel_A").animate({left:"-=1000px"},1400,'easeOutQuad',function(){
            })
            $(".carousel_B").animate({left:"-=1000px"},1400,'easeOutQuad',function(){
                    if(top_img==1)
                    {
                        $(".carousel_B").css("left","+=2000px")
                        $(".carousel_B>a>img").attr("src",img_array[inturn%6+1])
                        $(".carousel_B>a").attr("href",href_array[inturn++%6+1])
                        top_img=0
                    }
                    else if(top_img==0)
                    {
                        $(".carousel_A").css("left","+=2000px")
                        $(".carousel_A>a>img").attr("src",img_array[inturn%6+1])
                        $(".carousel_A>a").attr("href",href_array[inturn++%6+1])
                        top_img=1
                    }
            })
            t=setTimeout("carousel()",<?php echo $instance["period"];?>)
            }
            carousel();
        </script>
        <?php
    }
    function update($new_instance, $old_instance) {
        //$instance = $old_instance;
        //$instance['img_array'] = strip_tags(stripslashes($new_instance['img_array']));
        for ($i=1; $i<6; $i++)
        {
            $instance['img'.$i]=$new_instance['img'.$i];
            $instance['href'.$i]=$new_instance['href'.$i];
        }
        $instance['period']=$new_instance['period'];
        return $instance;
    }
    function form($instance) {
        global $wpdb;
        $instance = wp_parse_args((array) $instance, array(
            'img1'=>'http://fmn.rrimg.com/fmn057/xiaozhan/20120502/1800/x_large_QnCa_7cd600002fa81263.jpg',
            'img2'=>'http://fmn.rrimg.com/fmn060/xiaozhan/20120502/1800/x_large_z5D6_552b000013921263.jpg',
            'img3'=>'http://fmn.rrfmn.com/fmn058/xiaozhan/20120502/1800/x_large_j7cZ_2bc3000046c61263.jpg',
            'img4'=>'http://fmn.rrimg.com/fmn061/xiaozhan/20120502/1800/x_large_NSZ1_6da700000e7b1263.jpg',
            'img5'=>'http://fmn.rrimg.com/fmn059/xiaozhan/20120502/1800/x_large_Iegs_42fc000041031263.jpg',
            'href1'=>'http://www.baidu.com',
            'href2'=>'http://www.google.com',
            'href3'=>'http://www.renren.com',
            'href4'=>'http://www.zcool.com',
            'href5'=>'http://www.wordpress.org',
            'period'=>'5000'));
        //$limit = strip_tags($instance['limit']);
            ?>
                <label for="<?php echo $this->get_field_id('img'.$i); ?>">间隔时间：
                    <input class="widefat" id="<?php echo $this->get_field_id('period'); ?>"name="<?php echo $this->get_field_name('period'); ?>" type="text" value="<?php echo $instance['period']; ?>" />
                </label>
            <?php
        for ($i=1; $i<6; $i++) {?>
            <p>
                <label for="<?php echo $this->get_field_id('img'.$i); ?>">轮播图片#<?php echo $i;?>：
                    <input class="widefat" id="<?php echo $this->get_field_id('img'.$i); ?>"name="<?php echo $this->get_field_name('img'.$i); ?>" type="text" value="<?php echo $instance['img'.$i]; ?>" />
                </label>
                                <label for="<?php echo $this->get_field_id('href'.$i); ?>">图片链接#<?php echo $i;?>：
                    <input class="widefat" id="<?php echo $this->get_field_id('href'.$i); ?>"name="<?php echo $this->get_field_name('href'.$i); ?>" type="text" value="<?php echo $instance['href'.$i]; ?>" />
                </label>
            </p>
        <?php }
    }
}
add_action('widgets_init', 'widget_img_carousel_init');
function widget_img_carousel_init() {
    register_widget('widget_img_carousel');
}
?>
